package org.jin.samples.service.impl;

import java.util.List;
import org.jin.samples.dao.ISubjectDao;
import org.jin.samples.dao.ITestDao;
import org.jin.samples.dao.ITestTypeDao;
import org.jin.samples.model.ExamSubject;
import org.jin.samples.model.ExamTest;
import org.jin.samples.model.ExamTestType;
import org.jin.samples.model.vo.ExamSubjectVO;
import org.jin.samples.model.vo.ExamTestTypeVO;
import org.jin.samples.model.vo.ExamTestVO;
import org.jin.samples.page.PagerModel;
import org.jin.samples.service.ITestService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class TestServiceImpl implements ITestService{
	
	@Autowired
	private ITestDao testDao;
	
	@Autowired
	private ISubjectDao subjectDao;
	
	@Autowired
	private ITestTypeDao testTypeDao;
	
	
	public PagerModel<ExamTest> findAllByPage(){
		return testDao.findAllByPage();
	}
	
	@Override
	public List<ExamTest> findAllTest() {
		return testDao.findAllTest();
	}

	@Override
	public ExamTestVO findByTest(Integer id) {
		ExamTest examTest = testDao.findByTest(id);
		if(examTest!=null){
		ExamTestVO vo = new ExamTestVO();
		BeanUtils.copyProperties(examTest, vo);
		
		ExamSubject examSubject = subjectDao.get(examTest.getExamSubject().getSubId());
	
		if(examSubject!=null){
			vo.setName(examSubject.getName());
		}
	
		ExamTestTypeVO typeVo = new ExamTestTypeVO();
		BeanUtils.copyProperties(examTest.getExamTestType(), typeVo);
		vo.setExamTestTypeVo(typeVo);
		
		ExamSubjectVO subjectVO =new ExamSubjectVO();
		BeanUtils.copyProperties(examTest.getExamSubject(), subjectVO);
		vo.setSubId(subjectVO.getSubId());
		 return vo ;
		}
		 return null;
	}

	@Override
	public void addTest(ExamTestVO vo) throws Exception {
		ExamTest examTest = new ExamTest();
		BeanUtils.copyProperties(vo, examTest);
		ExamSubject examSubject = new ExamSubject();
		examSubject.setSubId(vo.getSubId());
		examTest.setExamSubject(examSubject);
		
		ExamTestType eamTestType = new ExamTestType();
		eamTestType.setId(vo.getExamTestTypeVo().getId());
		examTest.setExamTestType(eamTestType);
		
		testDao.save(examTest);
	}

	@Override
	public void editTest(ExamTestVO vo) throws Exception {
		
		ExamTest examTest = testDao.findByTest(vo.getId());
		if(examTest!=null){
			BeanUtils.copyProperties(vo, examTest);
			ExamSubject examSubject = new ExamSubject();
			examSubject.setSubId(vo.getSubId());
			examTest.setExamSubject(examSubject);
			
			ExamTestType eamTestType = new ExamTestType();
			eamTestType.setId(vo.getExamTestTypeVo().getId());
			examTest.setExamTestType(eamTestType);
			
			testDao.editTest(examTest);
		}		
	}


	@Override
	public void deleteUser(Integer id) {
		ExamTest examTest = testDao.findByTest(id);
		if(examTest!=null){
			testDao.deleteByKey(id);
		}
	}
	
	public List<ExamTest> findAllBySubId(Integer subId)throws Exception{
		return testDao.findAllBySubId(subId);
	}
	
	
	
	
	/*public Map<Integer,List<ExamTestVO>> getTestListBySubject(Integer subId, String userId){
		List<ExamTestType> testTypeList = testTypeDao.loadAll();
		Map<Integer,Integer> testTypeScoreMap = new HashMap<Integer,Integer>();//所有题型各自的分数
		Map<Integer, List<ExamTestVO>> testListMap = new HashMap<Integer, List<ExamTestVO>>();
		if(testTypeList != null){
			for(ExamTestType testType : testTypeList){
				testTypeScoreMap.put(testType.getId(), testType.getTestTypeScore());
				//3.0 根据科目获取各个题型所有题目    ??
				List<ExamTest> testList = testDao.getTestListBySubAndTestType(subId, testType.getId());
				List<ExamTestVO> testVOList = new ArrayList<ExamTestVO>();
				//分别获取各个题目在数据库里的所有题目
				for(ExamTest test : testList){
					ExamTestVO vo = new ExamTestVO();
					BeanUtils.copyProperties(test, vo);
					
					ExamTestTypeVO TestTypeVO = new ExamTestTypeVO();
					BeanUtils.copyProperties(test.getExamTestType(),TestTypeVO);
					vo.setExamTestTypeVo(TestTypeVO);
					testVOList.add(vo);		
				}
				testListMap.put(testType.getId(), testVOList);
			}
		}else{
			return null;
		}
		
		ExamSubject subject = subjectDao.get(subId);
		if(subject == null){
			return null;
		}	
		Integer totalTestScore = subject.getTotalScore().intValue();
		Integer totalTestCount = subject.getTestCount();
		
		//1.校验科目总分和总题数是否匹配
		if(!TestUtils.c)
		
		
		return null;
	}*/
	
	
	

}
